#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
状态管理控制器
整合路由和控制器功能
"""

from flask import Blueprint, jsonify, current_app
from services.app_service import AppService
from settings import get_logger

# 创建蓝图
status_bp = Blueprint('status', __name__, url_prefix='/api')

class StatusController:
    """
    状态管理控制器
    整合路由和控制器功能
    """
    
    def __init__(self):
        self.app_service = AppService()
        self.logger = get_logger(__name__)
        self._register_routes()
    
    def _register_routes(self):
        """
        注册所有路由
        """
        # 注册路由装饰器
        status_bp.route('/status', methods=['GET'])(self.get_status)
    
    def get_status(self):
        """
        获取服务状态
        
        Returns:
            dict: 状态信息
        """
        self.logger.info("获取服务状态")
        try:
            app_instance = self.app_service.get_app_instance()
            
            if not app_instance:
                result = {
                    "success": False,
                    "error": "微信后端应用未初始化",
                    "status_code": 500
                }
                return jsonify(result), result.get('status_code', 500)
            
            status = app_instance.get_status()
            result = {
                "success": True,
                "data": status
            }
            return jsonify(result), 200
            
        except Exception as e:
            self.logger.error(f"获取状态失败: {e}")
            result = {
                "success": False,
                "error": str(e),
                "status_code": 500
            }
            return jsonify(result), result.get('status_code', 500)


# 实例化控制器，确保路由被注册
status_controller = StatusController()